package com.fujin.gulimall.product.web;

import com.fujin.gulimall.product.Vo.Catelog2Vo;
import com.fujin.gulimall.product.entity.CategoryEntity;
import com.fujin.gulimall.product.service.CategoryService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @title: IndexController
 * @Author Tan
 * @Date: 2022/2/2 22:04
 * @Version 1.0
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping({"/","/index"})
    public String Index(Model model){
        //查询所有一级分类
        List<CategoryEntity> list = categoryService.getLevel1Catagories();

        model.addAttribute("catagories",list);
        return "index";
    }


    //index/json/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String ,List<Catelog2Vo>> getCatelogJson(){
        Map<String ,List<Catelog2Vo>> obj = categoryService.getCatelogJson();
        return obj;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        RLock lock = redissonClient.getLock("my-lock");

        try {
            //加锁
//            lock.lock();
            lock.lock(10, TimeUnit.SECONDS);
            //暂停30s，模拟处理业务时间
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //解锁
            lock.unlock();
        }
        return "Hello";
    }
}
